// JavaScript Document
//气球碎片
var debrisObj = function(){
    this.ctx = ctxMain;
    this.a = [];
    this.b = [];
    this.c = [];
    this.r = [];//大小
    this.max = [];//最远的距离
    this.ang = [];//角度
    this.fill = [];
    this.speed = [];
    this.alive = [];
};
debrisObj.prototype.num = 25;
debrisObj.prototype.currentNum = 0;
debrisObj.prototype.total = 250;
debrisObj.prototype.init = function() {
    for(var i = 0; i < this.total; i++) {
        this.alive[i] = false;
        this.r[i] = 20;
        this.speed[i] = 3;
    }
};
debrisObj.prototype.draw = function() {
    this.ctx.save();
    this.ctx.lineWidth = 2;
    this.ctx.shadowBlur = 10;
    this.ctx.shadowColor = "rgb(203,91,0)";
    for(var i = 0; i < this.total; i++) {
        if(this.alive[i]) {
            var moveX = this.speed[i]*Math.cos(this.ang[i]);
            var moveY = this.speed[i]*Math.sin(this.ang[i]);
            this.a[i].x += moveX;
            this.a[i].y += moveY;

            this.b[i].x += moveX;
            this.b[i].y += moveY;

            this.c[i].x += moveX;
            this.c[i].y += moveY;
            if(this.max[i] <= 0){
                this.alive[i] = false;
                break;
            }
            this.max[i] -= this.speed[i];
            var alpha = this.max[i]/200;
            this.ctx.beginPath();
            this.ctx.moveTo(this.a[i].x, this.a[i].y);
            this.ctx.lineTo(this.b[i].x, this.b[i].y);
            this.ctx.lineTo(this.c[i].x, this.c[i].y);
            this.ctx.closePath();
            //this.ctx.strokeStyle = "rgba(255,204,1," + alpha + ")";
            //this.ctx.stroke();
            this.ctx.fillStyle = "rgba(" + this.fill[i] + "," + alpha + ")";
            this.ctx.fill();
            if(this.max[i] <= this.speed[i]){
                this.alive[i] = false;
            }
        }
    }
    this.ctx.restore();
};
debrisObj.prototype.born = function(x, y, r, fill) {
    for(var i = 0; i < this.num; i++){
        var dot = dotInCircle(x, y, r), t = this.currentNum;
        this.a[t] = dotInCircle(dot.x, dot.y, this.r[i]);
        this.b[t] = dotInCircle(dot.x, dot.y, this.r[i]);
        this.c[t] = dotInCircle(dot.x, dot.y, this.r[i]);
        this.ang[t] = dot.ang;
        this.max[t] = 200;
        this.fill[t] = fill.substring(4, fill.length - 1);
        this.alive[t] = true;
        this.currentNum++;
        if(this.currentNum == this.total) this.currentNum = 0;
    }
};